import cache from "@/plugins/cache";

const state = {
  uploadFileLists:[]
}

const mutations = {
  INIT_UPLOAD: (state, obj) => {
    const index = state.uploadFileLists.findIndex(item=>obj.fileId ===item.fileId)
    if(index === -1){
      state.uploadFileLists.push(obj)
    }
  },
  UPDATE_UPLOAD: (state, obj) => {
    const index = state.uploadFileLists.findIndex(item=>obj.fileId ===item.fileId)
    if(index !== -1){
      state.uploadFileLists[index].progress = obj.progress
      state.uploadFileLists[index].isUploaded = obj.isUploaded
    }
    // console.log(state.uploadFileLists)
    // console.log(obj)
  },
  CLEAR_UPLOAD:(state, obj)=>{
    state.uploadFileLists=obj
  }

}

const actions = {
  //初始化进度
  initUploadFile({ commit }, obj) {
    commit("INIT_UPLOAD", obj);
  },
  // 更新进度
  uploadUploadFile({ commit }, obj) {
    commit("UPDATE_UPLOAD", obj);
  },
  //清空进度
  clearUploadFile({ commit }, obj) {
    commit("CLEAR_UPLOAD", obj);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
